@echo off
setlocal enabledelayedexpansion

echo === protoc-gen-msg-handler Example Build Script ===
echo This script demonstrates how to use protoc-gen-msg-handler
echo.

REM Check if protoc is available
where protoc >nul 2>&1
if %ERRORLEVEL% NEQ 0 (
    echo ❌ Error: protoc is not installed or not in PATH
    echo Please install Protocol Buffers compiler ^(protoc^) first
    pause
    exit /b 1
)

for /f "tokens=*" %%i in ('protoc --version') do set PROTOC_VERSION=%%i
echo ✓ protoc found: !PROTOC_VERSION!

REM Check if the plugin binary exists
set PLUGIN_NAME=protoc-gen-msg-handler.exe
if not exist "..\%PLUGIN_NAME%" (
    echo ❌ Error: %PLUGIN_NAME% not found in parent directory
    echo Please ensure the plugin binary is available
    pause
    exit /b 1
)

echo ✓ Plugin found: ..\%PLUGIN_NAME%
echo.

REM Create output directories
echo 📁 Creating output directories...
if not exist "output" mkdir output
if not exist "output\go" mkdir output\go
if not exist "output\csharp" mkdir output\csharp  
if not exist "output\typescript" mkdir output\typescript

REM Generate Go code
echo 🚀 Generating Go code...
protoc --plugin=..\%PLUGIN_NAME% --msg-handler_out=./output/go --msg-handler_opt=lang=go --proto_path=. proto/game.proto
if %ERRORLEVEL% NEQ 0 (
    echo ❌ Failed to generate Go code
    pause
    exit /b 1
)
echo ✅ Go code generated successfully

REM Generate C# code
echo 🚀 Generating C# code...
protoc --plugin=..\%PLUGIN_NAME% --msg-handler_out=./output/csharp --msg-handler_opt=lang=csharp,csharp_namespace=Example.Game --proto_path=. proto/game.proto
if %ERRORLEVEL% NEQ 0 (
    echo ❌ Failed to generate C# code
    pause
    exit /b 1
)
echo ✅ C# code generated successfully

REM Generate TypeScript code  
echo 🚀 Generating TypeScript code...
protoc --plugin=..\%PLUGIN_NAME% --msg-handler_out=./output/typescript --msg-handler_opt=lang=typescript --proto_path=. proto/game.proto
if %ERRORLEVEL% NEQ 0 (
    echo ❌ Failed to generate TypeScript code
    pause
    exit /b 1
)
echo ✅ TypeScript code generated successfully

echo.
echo 🎉 All code generation completed successfully!
echo.
echo Generated files:
echo 📂 Go files:
for /r "output\go" %%f in (*.go) do echo    %%f
echo 📂 C# files:
for /r "output\csharp" %%f in (*.cs) do echo    %%f
echo 📂 TypeScript files:
for /r "output\typescript" %%f in (*.ts) do echo    %%f
echo.
echo You can now use these generated files in your projects!
echo See README.md for integration examples.
echo.
pause